package com.newedu.equipmgr.servlet.equipment.area;

import com.alibaba.fastjson.JSONObject;
import com.newedu.equipmgr.entity.AreaInfo;
import com.newedu.equipmgr.service.AreaService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author jerry
 * @create 2020-06-08 14:06:20
 * @company 新开普电子股份有限公司
 */
@WebServlet(name="AreaAddServlet",urlPatterns = {"/api/servlet/area/add"})
public class AreaAddServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.doPost(req, resp);
        String building = req.getParameter("building");
        String floor = req.getParameter("floor");


        AreaService areaService = new AreaService();
        boolean insert = areaService.insert(new AreaInfo(building, floor));
        JSONObject jsonObject = new JSONObject();
        // {"message": "添加成功", "status":"success"}
        if(insert){//成功 {"message": "添加成功", "status":"success"}
            jsonObject.put("message","添加成功");
            jsonObject.put("status","success");

        }else{// 失败 {"message": "添加失败", "status":"fail"}
            jsonObject.put("message","添加失败");
            jsonObject.put("status","fail");
        }

        resp.setContentType("application/json;charset=utf-8");
        String jsonString = jsonObject.toJSONString();
        System.out.println(jsonString);
        resp.getWriter().println(jsonString);
    }
}
